﻿##################################################
## Matchmaker Contract
## laamp_regula_matchmaker
##
## A local ruler hires the player to find a suitable match for their unmarried
## daughter. The player travels to the employer's holding and uses diplomacy
## to arrange a marriage. The contract uses a scheme-based progression mechanic
## with monthly flavor events providing narrative color.
##
## Variables on the contract:
## daughter               - the employer's unmarried daughter (scheme target)
## gold_success_standard  - pre-calculated gold for standard success
## gold_success_critical  - pre-calculated gold for critical success (safety margin)
##
## Group: laamp_contracts_diplomacy_group
## Scheme: regula_matchmaker_contract_scheme
## Events: regula_matchmaker_contract namespace
##################################################
laamp_regula_matchmaker = {
	group = laamp_contracts_diplomacy_group
	icon = "gfx/interface/icons/task_contracts/regula_icon_task_contract_matchmaker.dds"

	travel = yes
	use_diplomatic_range = no

	weight = {
		# Standard weights.
		value = task_contract_weight_default_value
		add = laamp_contracts_weight_up_diplomacy_value
		add = laamp_contracts_weight_up_regula_value
		# Employer weights.
		scope:employer = {
			add = {
				# Weight up.
				## Traits that are socially active and eager to see their daughter married well.
				if = {
					limit = { has_trait = gregarious }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = ambitious }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = diligent }
					add = task_contract_weight_bonus_employer_has_associated_traits_value
				}
				# Weight down.
				## Traits that would not bother hiring a matchmaker.
				if = {
					limit = { has_trait = shy }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = lazy }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				if = {
					limit = { has_trait = content }
					add = task_contract_weight_malus_employer_has_associated_traits_value
				}
				# Restrictions.
				min = task_contract_weight_employer_associated_traits_min_value
				max = task_contract_weight_employer_associated_traits_max_value
			}
			multiply = task_contract_weight_by_tier_value
			# This weight is an override, so make sure it's placed at the bottom.
			add = task_contract_weight_employer_contact_list_value
		}
	}

	# Validity Triggers
	valid_to_create = {
		# Game rule gate — respect lewd contract availability setting.
		regula_lewd_contracts_available_trigger = yes
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		employer_has_treasury_to_offer_job_trigger = yes
		valid_laamp_sensible_start_trigger = yes
		# Employer must have at least one unmarried, unbetrothed adult daughter at court.
		scope:employer = {
			any_child = {
				regula_matchmaker_valid_daughter_candidate_trigger = { EMPLOYER = scope:employer }
			}
		}
	}
	valid_to_accept = {
		# Standard triggers.
		valid_laamp_basic_accept_only_trigger = yes
		valid_laamp_basic_trigger = {
			EMPLOYER = scope:employer
			LAAMP = root
		}
		# Player must be an adult male
		is_adult = yes
		OR = {
			carn_is_futa_trigger = yes
			is_male = yes
		}
	}
	valid_to_continue = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_taker = { is_adult = yes }
		# The daughter must still be alive and unmarried.
		var:daughter = {
			is_alive = yes
			is_married = no
			is_betrothed = no
			NOT = { has_trait = pregnant }
		}
		# Must have at least one suitor remaining.
		variable_list_size = { name = suitors value >= 1 }
	}
	valid_to_keep = {
		# Standard triggers.
		valid_laamp_basic_trigger = {
			EMPLOYER = root.task_contract_employer
			LAAMP = root.task_contract_taker
		}
		task_contract_employer = { is_landed = yes }
		task_contract_taker = { is_adult = yes }
		# The daughter must still be alive and unmarried.
		var:daughter = {
			is_alive = yes
			is_married = no
			is_betrothed = no
		}
	}

	# On_actions
	on_create = {
		scope:contract = {
			save_scope_as = task_contract
			task_contract_employer = {
				save_scope_as = employer
				random_child = {
					limit = {
						regula_matchmaker_valid_daughter_candidate_trigger = { EMPLOYER = prev }
					}
					save_scope_as = daughter
				}
			}
			set_variable = { name = daughter value = scope:daughter }
			set_task_contract_target = scope:daughter
			# Setup gold rewards.
			regula_scheme_contract_setup_gold_effect = yes
			# Gather suitor candidates for the contract.
			regula_matchmaker_gather_suitors_effect = yes
		}
	}
	on_accepted = {
		task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_accept_contract" }
		# For the intro event.
		save_scope_as = task_contract
		task_contract_employer = { save_scope_as = employer }
		# Start the scheme against the daughter.
		task_contract_taker ?= {
			start_scheme = {
				type = regula_matchmaker_contract_scheme
				contract = root
				target_character = root.var:daughter
			}
			scope:task_contract.scheme = { save_scope_as = scheme }
			# Block daughter from marriage while contract is active.
			root.var:daughter = {
				set_variable = { name = can_not_marry value = yes }
			}
			trigger_event = regula_matchmaker_contract.0001
		}
	}
	on_invalidated = {
		if = {
			limit = {
				root.task_contract_taker = {
					any_character_active_contract = { this = root }
				}
			}
			# Save scopes for invalidation.
			save_scope_as = task_contract
			task_contract_employer = { save_scope_as = employer }

			# Remove marriage block from daughter before invalidating.
			if = {
				limit = {
					has_variable = daughter
					var:daughter = {
						has_variable = can_not_marry
					}
				}
				var:daughter = { remove_variable = can_not_marry }
			}

			# Daughter pregnant — this FAILS the contract, not just invalidates.
			if = {
				limit = {
					has_variable = daughter
					var:daughter = { has_trait = pregnant }
				}
				task_contract_taker = {
					trigger_event = regula_matchmaker_contract.0080
				}
			}
			# All other reasons — standard invalidation event.
			else = {
				# Determine invalidation reason and set flag.
				if = {
					limit = {
						task_contract_employer = { is_alive = no }
					}
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:employer_dead }
					}
				}
				else_if = {
					limit = {
						task_contract_employer = { is_landed = no }
					}
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:employer_unlanded }
					}
				}
				else_if = {
					limit = {
						has_variable = daughter
						var:daughter = { is_alive = no }
					}
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:daughter_dead }
					}
				}
				else_if = {
					limit = {
						has_variable = daughter
						var:daughter = {
							OR = {
								is_married = yes
								is_betrothed = yes
							}
						}
					}
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:daughter_married }
					}
				}
				else_if = {
					limit = {
						NOT = { variable_list_size = { name = suitors value >= 1 } }
					}
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:no_suitors }
					}
				}
				else = {
					task_contract_taker = {
						save_scope_value_as = { name = invalidation_reason value = flag:unknown }
					}
				}

				# Fire standard invalidation event.
				task_contract_taker = {
					trigger_event = regula_matchmaker_contract.0081
				}
			}
		}
	}

	# Rewards
	task_contract_reward = {
		success_standard = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				# Camp building extra Diplomacy Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_diplomacy_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_standard
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
				task_contract_regula_contract_tracker_effect = yes
			}
		}
		success_recruitment = {
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_success_on_contract" }
				# Camp building extra Diplomacy Rewards: +50%.
				if = {
					limit = {
						task_contract_taker.domicile ?= { has_domicile_parameter = camp_improved_diplomacy_contract_rewards }
					}
					save_scope_value_as = { name = extra_reward value = flag:yes }
				}
				else = {
					save_scope_value_as = { name = extra_reward value = flag:no }
				}

				show_as_tooltip = {
					task_contract_taker = {
						add_courtier = root.var:daughter
					}
				}

				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = scope:task_contract.var:gold_success_recruitment
					PRESTIGE = task_contract_success_prestige_gain_half_value
					PIETY = 0
					PROVISIONS = task_contract_success_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_reward_value
					OPINION_TYPE = succeeded_task_contract_opinion
					CONTACT = yes
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
				task_contract_taker = { task_successful_noncrim_contract_count_effect = yes }
				task_contract_regula_contract_tracker_effect = yes
			}
		}
		failure_standard = {
			positive = no
			should_print_on_complete = yes
			effect = {
				task_contract_taker = { play_sound_effect = "event:/DLC/EP3/SFX/UI/Contracts/ep3_ui_contracts_failure_on_contract" }
				save_scope_value_as = { name = extra_reward value = flag:no }
				# Standard Penalties.
				laamp_rewards_disburse_ordinary_currency_effect = {
					GOLD = 0
					PRESTIGE = task_contract_failure_prestige_loss_full_value
					PIETY = 0
					PROVISIONS = task_contract_failure_provisions_gain_minor_value
					OPINION = task_contract_opinion_standard_failure_value
					OPINION_TYPE = failed_task_contract_opinion
					CONTACT = no
					CONTACT_HOOK = no
					EXTRA_REWARD = scope:extra_reward
				}
			}
		}
	}
}
